home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / usr / sybase / sample / dblibrary / example4.c < prev    next >
C/C++ Source or Header  |  1993-04-22  |  5KB  |  232 lines

  1. /*
  2. **    example4.c
  3. **
  4. ** This example accesses the data within each row without using dbbind(),
  5. ** and illustrates the use of row buffering.
  6. **
  7. ** It runs a query, saves all of the returned rows (up to a maximum
  8. ** of 1000) using DB-Library row buffering, and allows the user to
  9. ** examine data rows at random.
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <sybfront.h>
  14. #include <sybdb.h>
  15.  
  16. extern char          *malloc();
  17.  
  18. DBPROCESS            *dbproc;  /* Our connection with SQL Server. */
  19. LOGINREC             *login;   /* Our login information. */
  20.  
  21. #define TYPELEN 2
  22.  
  23. /* Forward declarations of the error handler and message handler. */
  24. int                  err_handler();
  25. int                  msg_handler();
  26.  
  27.  
  28. main(argc, argv)
  29. int                  argc;
  30. char                 *argv[];
  31. {
  32.     /* Here are the variables which will be used to store the
  33.      * data being examined.
  34.      */
  35.     DBCHAR             name[MAXNAME+1];  /* MAXNAME is defined in
  36.                                           * "sybdb.h" as the maximum
  37.                                           * length for names of database
  38.                                           * objects, such as tables,
  39.                                           * columns, and procedures.
  40.                                          */
  41.     DBCHAR             type[TYPELEN+1];
  42.     DBINT              id;
  43.  
  44.     DBCHAR             datebuf[64];
  45.     DBINT              len;
  46.     char               numstring[32];
  47.     int                quitflag = 0;
  48.     RETCODE            row_code;
  49.     int                rownum;
  50.  
  51.     /* Initialize DB-Library. */
  52.     if (dbinit() == FAIL)
  53.         exit(ERREXIT);
  54.  
  55.     /* Install the user-supplied error-handling and message-handling
  56.      * routines. They are defined at the bottom of this source file.
  57.      */
  58.     dberrhandle(err_handler);
  59.     dbmsghandle(msg_handler);
  60.  
  61.     /*
  62.     ** Get a LOGINREC structure and fill it with the necessary
  63.     ** login information.
  64.     */
  65.  
  66.     login = dblogin();
  67.     DBSETLPWD(login, "server_password");
  68.     DBSETLAPP(login, "example4");
  69.  
  70.     dbproc = dbopen(login, NULL);
  71.  
  72.     dbcmd(dbproc, "select name, type, id, crdate from sysobjects");
  73.     dbcmd(dbproc, " where type = 'S'");
  74.  
  75.     /*
  76.     ** Buffer the rows so we can read them any number of times.
  77.     ** Passing '0' as the number of rows to buffer will get us
  78.     ** default row buffering (currently 1000 rows).
  79.     ** If more than 1000 rows are received, this program will
  80.     ** only save the last 1000.
  81.     **
  82.     ** Note that this parameter must be passed as an ASCII string.
  83.     */
  84.  
  85.     dbsetopt(dbproc, DBBUFFER, "0");
  86.  
  87.     dbsqlexec(dbproc);
  88.  
  89.     if (dbresults(dbproc) == SUCCEED) 
  90.     {
  91.         /* Read all of the rows into DB-Library's buffer */
  92.  
  93.         while ((row_code = dbnextrow(dbproc)) != NO_MORE_ROWS)
  94.         {
  95.             /* If DB-Library's row buffer is full, throw
  96.              * away the oldest row, to allow the newest
  97.              * row to be read in.
  98.              */
  99.             if (row_code == BUF_FULL)
  100.                 dbclrbuf(dbproc, 1);
  101.         }
  102.     
  103.         /* Print out the column headers. */
  104.  
  105.         printf
  106.             (" %-20s %10s %25s %15s \n",
  107.              "       NAME         ",
  108.              "TYPE",
  109.              "           DATE          ",
  110.              "ID");
  111.         printf
  112.             (" %20s %10s %25s %15s \n",
  113.              "--------------------",
  114.              "----",
  115.              "----------------------",
  116.              "----");
  117.  
  118.         /* Let the user view any row in the table. */
  119.  
  120.         printf("Type the number of the row you want to see.\n");
  121.         printf("The first row is number 1.\n");
  122.         printf("Asking for row 0 will terminate the program.\n");
  123.  
  124.         while (quitflag == 0)
  125.         {
  126.             printf("Row number: ");
  127.             gets(numstring);
  128.             rownum = atoi(numstring);
  129.  
  130.             if (rownum == 0)
  131.                 quitflag = 1;
  132.             else
  133.             {
  134.                 /* Print out the requested row. */
  135.  
  136.                 if (dbgetrow(dbproc, rownum) == NO_MORE_ROWS)
  137.                     printf
  138.                         ("That row is not in the table.\n");
  139.                 else
  140.                 {
  141.                     /* Copy variable-length character data
  142.                      * (colname).
  143.                      */
  144.         
  145.                     strncpy
  146.                         (name, (DBCHAR *)dbdata(dbproc, 1),
  147.                          (len = dbdatlen(dbproc, 1)));
  148.  
  149.                     /* String needs terminating null. */
  150.  
  151.                     name[len] = '\0';
  152.  
  153.                     /* Copy fixed-length character data. */
  154.  
  155.                     strncpy
  156.                         (type, (DBCHAR *)dbdata(dbproc, 2),
  157.                          (len = dbdatlen(dbproc, 2)));
  158.                     type[len] = '\0';
  159.  
  160.                     /* Copy integer data. */
  161.  
  162.                     id = *((DBINT *)dbdata(dbproc, 3));
  163.  
  164.                     /* Convert datetime data to a printable
  165.                      * string.
  166.                      */
  167.  
  168.                     dbconvert(dbproc, SYBDATETIME, (dbdata(dbproc, 4)), 
  169.                           (DBINT)-1, SYBCHAR, datebuf, (DBINT)-1);
  170.                     printf
  171.                         ("%20s %10s %25s %15ld \n",
  172.                          name, type, datebuf, id);
  173.                 }
  174.             }
  175.         }
  176.  
  177.     }
  178.  
  179.     dbexit();
  180.     exit(STDEXIT);
  181. }
  182.  
  183. int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
  184. DBPROCESS       *dbproc;
  185. int             severity;
  186. int             dberr;
  187. int             oserr;
  188. char            *dberrstr;
  189. char            *oserrstr;
  190. {
  191.     if ((dbproc == NULL) || (DBDEAD(dbproc)))
  192.         return(INT_EXIT);
  193.     else 
  194.     {
  195.         printf("DB-Library error:\n\t%s\n", dberrstr);
  196.  
  197.         if (oserr != DBNOERR)
  198.             printf("Operating-system error:\n\t%s\n", oserrstr);
  199.  
  200.         return(INT_CANCEL);
  201.     }
  202. }
  203.  
  204.  
  205. int msg_handler(dbproc, msgno, msgstate, severity, msgtext, 
  206.                 srvname, procname, line)
  207.  
  208. DBPROCESS       *dbproc;
  209. DBINT           msgno;
  210. int             msgstate;
  211. int             severity;
  212. char            *msgtext;
  213. char            *srvname;
  214. char            *procname;
  215. DBUSMALLINT     line;
  216.  
  217. {
  218.     printf ("Msg %ld, Level %d, State %d\n", 
  219.             msgno, severity, msgstate);
  220.  
  221.     if (strlen(srvname) > 0)
  222.         printf ("Server '%s', ", srvname);
  223.     if (strlen(procname) > 0)
  224.         printf ("Procedure '%s', ", procname);
  225.     if (line > 0)
  226.         printf ("Line %d", line);
  227.  
  228.     printf("\n\t%s\n", msgtext);
  229.  
  230.     return(0);
  231. }
  232.